<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>qiankun源码解读-示例篇 | Pluto</title><meta name="author" content="拾壹baby"><meta name="copyright" content="拾壹baby"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="介绍qiankun 是基于 single-spa 做了二次封装的微前端框架，既然是二次封装，那么少不了在此基础上解决了一些 single-spa 的缺陷和不足。 single-spa 有哪些不足（其实也是qiankun的优点）1. 打包工具配置的不友好侵入型强其实说的就是第三点，更改打包工具的配置，使用 single-spa 接入微应用需要将微应用整个打包成一个 JS 文件，发布到静态资源服务器，">
<meta property="og:type" content="article">
<meta property="og:title" content="qiankun源码解读-示例篇">
<meta property="og:url" content="http://cdn.pluto1811.com/pluto/2022/09/05/qiankun01/">
<meta property="og:site_name" content="Pluto">
<meta property="og:description" content="介绍qiankun 是基于 single-spa 做了二次封装的微前端框架，既然是二次封装，那么少不了在此基础上解决了一些 single-spa 的缺陷和不足。 single-spa 有哪些不足（其实也是qiankun的优点）1. 打包工具配置的不友好侵入型强其实说的就是第三点，更改打包工具的配置，使用 single-spa 接入微应用需要将微应用整个打包成一个 JS 文件，发布到静态资源服务器，">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://cdn.pluto1811.com/goodsImage/1615966170678/1615966002577.jpg">
<meta property="article:published_time" content="2022-09-05T05:10:55.000Z">
<meta property="article:modified_time" content="2022-11-12T03:17:02.090Z">
<meta property="article:author" content="拾壹baby">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://cdn.pluto1811.com/goodsImage/1615966170678/1615966002577.jpg"><link rel="shortcut icon" href="/pluto/img/favicon.png"><link rel="canonical" href="http://cdn.pluto1811.com/pluto/2022/09/05/qiankun01/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/pluto/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/pluto/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'qiankun源码解读-示例篇',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-11-12 11:17:02'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/pluto/atom.xml" title="Pluto" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/pluto/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/pluto/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/pluto/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/pluto/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/pluto/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/pluto/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/pluto/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/pluto/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('http://cdn.pluto1811.com/goodsImage/1615966170678/1615966002577.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/pluto/">Pluto</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/pluto/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/pluto/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/pluto/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/pluto/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/pluto/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">qiankun源码解读-示例篇</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-09-05T05:10:55.000Z" title="发表于 2022-09-05 13:10:55">2022-09-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-11-12T03:17:02.090Z" title="更新于 2022-11-12 11:17:02">2022-11-12</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="qiankun源码解读-示例篇"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p><font color="#ec5d3" bgcolor="#fdf5f5">qiankun</font> 是基于 single-spa 做了二次封装的微前端框架，既然是二次封装，那么少不了在此基础上解决了一些 single-spa 的缺陷和不足。</p>
<h2 id="single-spa-有哪些不足（其实也是qiankun的优点）"><a href="#single-spa-有哪些不足（其实也是qiankun的优点）" class="headerlink" title="single-spa 有哪些不足（其实也是qiankun的优点）"></a>single-spa 有哪些不足（其实也是<font color="#ec5d3" bgcolor="#fdf5f5">qiankun</font>的优点）</h2><h3 id="1-打包工具配置的不友好"><a href="#1-打包工具配置的不友好" class="headerlink" title="1. 打包工具配置的不友好"></a>1. 打包工具配置的不友好</h3><p>侵入型强其实说的就是第三点，更改打包工具的配置，使用 <font color="#ec5d3" bgcolor="#fdf5f5">single-spa</font> 接入微应用需要将微应用整个打包成一个 JS 文件，发布到静态资源服务器，然后在主应用中配置该 JS 文件的地址告诉 <font color="#ec5d3" bgcolor="#fdf5f5">single-spa</font> 去这个地址加载微应用。</p>
<p>不说其它的，就现在这个改动就存在很大的问题，将整个微应用打包成一个 JS 文件，常见的打包优化基本上都没了，比如：按需加载、首屏资源加载优化、css 独立打包等优化措施。</p>
<h3 id="2-样式隔离问题"><a href="#2-样式隔离问题" class="headerlink" title="2. 样式隔离问题"></a>2. 样式隔离问题</h3><p><font color="#ec5d3" bgcolor="#fdf5f5">single-spa</font> 没有做这部分的工作。这时只能通过约定命名规范来实现，比如应用样式以自己的应用名称开头，以应用名构造一个独立的命名空间，这个方式新系统还好说，如果是一个已有的系统，这个改造工作量可不小。</p>
<h3 id="3-JS-隔离"><a href="#3-JS-隔离" class="headerlink" title="3. JS 隔离"></a>3. JS 隔离</h3><p>这部分工作 single-spa 也没有做。 JS 全局对象污染是一个很常见的现象，难以排查，有可能是灾难性的。</p>
<h3 id="4-资源预加载"><a href="#4-资源预加载" class="headerlink" title="4. 资源预加载"></a>4. 资源预加载</h3><p>这部分的工作<font color="#ec5d3" bgcolor="#fdf5f5">single-spa</font> 更没做了，因为<font color="#ec5d3" bgcolor="#fdf5f5">single-spa</font> 已经将微应用整个打包成了一个 js 文件。</p>
<h3 id="5-应用间通信"><a href="#5-应用间通信" class="headerlink" title="5. 应用间通信"></a>5. 应用间通信</h3><p>这部分工作 <font color="#ec5d3" bgcolor="#fdf5f5">single-spa</font> 没做，它只在注册微应用时给微应用注入一些状态信息，后续就不管了，没有任何通信的手段，只能用户自己去实现。</p>
<h2 id="系列文章"><a href="#系列文章" class="headerlink" title="系列文章"></a>系列文章</h2><blockquote>
<p><a target="_blank" rel="noopener" href="https://www.zybuluo.com/static/img/logo.png">微前端框架之源码示例项目与框架源码解读</a>，由源码最简单的部分入门，先搞懂怎么启动项目的，api 怎么调用的。带着问题，再去读源码事半功倍。</p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://www.zybuluo.com/static/img/logo.png">qiankun 2.x 运行时沙箱 源码分析</a>，详细解读了 qiankun 2.x 版本的沙箱实现</p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://www.zybuluo.com/static/img/logo.png">HTML Entry 源码分析</a>，详细解读了 HTML Entry 的原理以及在 qiankun 中的应用</p>
</blockquote>
<h2 id="框架目录结构"><a href="#框架目录结构" class="headerlink" title="框架目录结构"></a>框架目录结构</h2><p>从 <a target="_blank" rel="noopener" href="https://github.com/liyongning/qiankun">github</a> 克隆项目<br><img src="http://cdn.pluto1811.com/forEditor/1615968394334/1615968380013.jpg" alt="1615968380013.jpg"></p>
<hr>
<h4 id="不简单的-package-json"><a href="#不简单的-package-json" class="headerlink" title="不简单的 package.json"></a>不简单的 package.json</h4><ul>
<li><p><a target="_blank" rel="noopener" href="https://www.npmjs.com/package/npm-run-all">npm-run-all</a></p>
<blockquote>
<p>一个 CLI 工具，用于并行或顺序执行多个 npm 脚本</p>
</blockquote>
</li>
<li><p><a target="_blank" rel="noopener" href="https://www.npmjs.com/package/father-build">father-build</a></p>
<blockquote>
<p>基于 rollup 的库构建工具，father 更加强大</p>
</blockquote>
</li>
<li><p>多项目的目录组织以及 scripts 部分的编写</p>
</li>
<li><p>main 和 module 字段</p>
<blockquote>
<p>标识组件库的入口，当两者同时存在时，module 字段的优先级高于 main</p>
</blockquote>
</li>
</ul>
<h2 id="示例项目"><a href="#示例项目" class="headerlink" title="示例项目"></a>示例项目</h2><h3 id="主应用"><a href="#主应用" class="headerlink" title="主应用"></a>主应用</h3><p>主应用在 <font color="#ec5d3" bgcolor="#fdf5f5">examples&#x2F;main</font> 目录下，提供了两种实现方式，基于路由配置的 <font color="#ec5d3" bgcolor="#fdf5f5">registerMicroApps</font> 和 手动加载微应用的 <font color="#ec5d3" bgcolor="#fdf5f5">loadMicroApp</font>。主应用很简单，就是一个从 0 通过 webpack 配置的一个同时支持 react 和 vue 的项目，至于为什么同时支持 react 和 vue，继续往下看</p>
<h4 id="webpack-config-js"><a href="#webpack-config-js" class="headerlink" title="webpack.config.js"></a>webpack.config.js</h4><p>就是一个普通的 webpack 配置，配置了一个开发服务器 devServer、两个 loader (babel-loader、css loader)、一个插件 HtmlWebpackPlugin (告诉 webpack html 模版文件是哪个)<br>通过 webpack 配置文件的 entry 字段得知入口文件分别为 <font color="#ec5d3" bgcolor="#fdf5f5">index.js</font> 和 <font color="#ec5d3" bgcolor="#fdf5f5">multiple.js</font></p>
<h4 id="基于路由配置"><a href="#基于路由配置" class="headerlink" title="基于路由配置"></a>基于路由配置</h4><p>通用将微应用关联到一些 url 规则的方式，实现当浏览器 url 发生变化时，自动加载相应的微应用的功能</p>
<h4 id="index-js"><a href="#index-js" class="headerlink" title="index.js"></a>index.js</h4><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">// qiankun api 引入</span>
<span class="token keyword">import</span> <span class="token punctuation">&#123;</span>
  registerMicroApps<span class="token punctuation">,</span>
  runAfterFirstMounted<span class="token punctuation">,</span>
  setDefaultMountApp<span class="token punctuation">,</span>
  start<span class="token punctuation">,</span>
  initGlobalState<span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span> <span class="token keyword">from</span> <span class="token string">"../../es"</span><span class="token punctuation">;</span>
<span class="token comment">// 全局样式</span>
<span class="token keyword">import</span> <span class="token string">"./index.less"</span><span class="token punctuation">;</span>

<span class="token comment">// 专门针对 angular 微应用引入的一个库</span>
<span class="token keyword">import</span> <span class="token string">"zone.js"</span><span class="token punctuation">;</span>

<span class="token comment">/**
 * 主应用可以使用任何技术栈，这里提供了 react 和 vue 两种，可以随意切换
 * 最终都导出了一个 render 函数，负责渲染主应用
 */</span>
<span class="token comment">// import render from './render/ReactRender';</span>
<span class="token keyword">import</span> render <span class="token keyword">from</span> <span class="token string">"./render/VueRender"</span><span class="token punctuation">;</span>

<span class="token comment">// 初始化主应用，其实就是渲染主应用</span>
<span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span> <span class="token literal-property property">loading</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 定义 loader 函数，切换微应用时由 qiankun 框架负责调用显示一个 loading 状态</span>
<span class="token keyword">const</span> <span class="token function-variable function">loader</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">loading</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span> loading <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 注册微应用</span>
<span class="token function">registerMicroApps</span><span class="token punctuation">(</span>
  <span class="token comment">// 微应用配置列表</span>
  <span class="token punctuation">[</span>
    <span class="token punctuation">&#123;</span>
      <span class="token comment">// 应用名称</span>
      <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">"react16"</span><span class="token punctuation">,</span>
      <span class="token comment">// 应用的入口地址</span>
      <span class="token literal-property property">entry</span><span class="token operator">:</span> <span class="token string">"//localhost:7100"</span><span class="token punctuation">,</span>
      <span class="token comment">// 应用的挂载点，这个挂载点在上面渲染函数中的模版里面提供的</span>
      <span class="token literal-property property">container</span><span class="token operator">:</span> <span class="token string">"#subapp-viewport"</span><span class="token punctuation">,</span>
      <span class="token comment">// 微应用切换时调用的方法，显示一个 loading 状态</span>
      loader<span class="token punctuation">,</span>
      <span class="token comment">// 当路由前缀为 /react16 时激活当前应用</span>
      <span class="token literal-property property">activeRule</span><span class="token operator">:</span> <span class="token string">"/react16"</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#123;</span>
      <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">"react15"</span><span class="token punctuation">,</span>
      <span class="token literal-property property">entry</span><span class="token operator">:</span> <span class="token string">"//localhost:7102"</span><span class="token punctuation">,</span>
      <span class="token literal-property property">container</span><span class="token operator">:</span> <span class="token string">"#subapp-viewport"</span><span class="token punctuation">,</span>
      loader<span class="token punctuation">,</span>
      <span class="token literal-property property">activeRule</span><span class="token operator">:</span> <span class="token string">"/react15"</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#123;</span>
      <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">"vue"</span><span class="token punctuation">,</span>
      <span class="token literal-property property">entry</span><span class="token operator">:</span> <span class="token string">"//localhost:7101"</span><span class="token punctuation">,</span>
      <span class="token literal-property property">container</span><span class="token operator">:</span> <span class="token string">"#subapp-viewport"</span><span class="token punctuation">,</span>
      loader<span class="token punctuation">,</span>
      <span class="token literal-property property">activeRule</span><span class="token operator">:</span> <span class="token string">"/vue"</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#123;</span>
      <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">"angular9"</span><span class="token punctuation">,</span>
      <span class="token literal-property property">entry</span><span class="token operator">:</span> <span class="token string">"//localhost:7103"</span><span class="token punctuation">,</span>
      <span class="token literal-property property">container</span><span class="token operator">:</span> <span class="token string">"#subapp-viewport"</span><span class="token punctuation">,</span>
      loader<span class="token punctuation">,</span>
      <span class="token literal-property property">activeRule</span><span class="token operator">:</span> <span class="token string">"/angular9"</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#123;</span>
      <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">"purehtml"</span><span class="token punctuation">,</span>
      <span class="token literal-property property">entry</span><span class="token operator">:</span> <span class="token string">"//localhost:7104"</span><span class="token punctuation">,</span>
      <span class="token literal-property property">container</span><span class="token operator">:</span> <span class="token string">"#subapp-viewport"</span><span class="token punctuation">,</span>
      loader<span class="token punctuation">,</span>
      <span class="token literal-property property">activeRule</span><span class="token operator">:</span> <span class="token string">"/purehtml"</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  <span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token comment">// 全局生命周期钩子，切换微应用时框架负责调用</span>
  <span class="token punctuation">&#123;</span>
    <span class="token literal-property property">beforeLoad</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">(</span><span class="token parameter">app</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 这个打印日志的方法可以学习一下，第三个参数会替换掉第一个参数中的 %c%s，并且第三个参数的颜色由第二个参数决定</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"[LifeCycle] before load %c%s"</span><span class="token punctuation">,</span> <span class="token string">"color: green;"</span><span class="token punctuation">,</span> app<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token literal-property property">beforeMount</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">(</span><span class="token parameter">app</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"[LifeCycle] before mount %c%s"</span><span class="token punctuation">,</span> <span class="token string">"color: green;"</span><span class="token punctuation">,</span> app<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token literal-property property">afterUnmount</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">(</span><span class="token parameter">app</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>
          <span class="token string">"[LifeCycle] after unmount %c%s"</span><span class="token punctuation">,</span>
          <span class="token string">"color: green;"</span><span class="token punctuation">,</span>
          app<span class="token punctuation">.</span>name
        <span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 定义全局状态，并返回两个通信方法</span>
<span class="token keyword">const</span> <span class="token punctuation">&#123;</span> onGlobalStateChange<span class="token punctuation">,</span> setGlobalState <span class="token punctuation">&#125;</span> <span class="token operator">=</span> <span class="token function">initGlobalState</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
  <span class="token literal-property property">user</span><span class="token operator">:</span> <span class="token string">"qiankun"</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 监听全局状态的更改，当状态发生改变时执行回调函数</span>
<span class="token function">onGlobalStateChange</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> prev</span><span class="token punctuation">)</span> <span class="token operator">=></span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"[onGlobalStateChange - master]:"</span><span class="token punctuation">,</span> value<span class="token punctuation">,</span> prev<span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 设置新的全局状态，只能设置一级属性，微应用只能修改已存在的一级属性</span>
<span class="token function">setGlobalState</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
  <span class="token literal-property property">ignore</span><span class="token operator">:</span> <span class="token string">"master"</span><span class="token punctuation">,</span>
  <span class="token literal-property property">user</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">"master"</span><span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 设置默认进入的子应用，当主应用启动以后默认进入指定微应用</span>
<span class="token function">setDefaultMountApp</span><span class="token punctuation">(</span><span class="token string">"/react16"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 启动应用</span>
<span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 当第一个微应用挂载以后，执行回调函数，在这里可以做一些特殊的事情，比如开启一监控或者买点脚本</span>
<span class="token function">runAfterFirstMounted</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"[MainApp] first app mounted"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<hr>
<h4 id="ReactRender-js"><a href="#ReactRender-js" class="headerlink" title="ReactRender.js"></a>ReactRender.js</h4><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">/**
 * 同 vue 实现的渲染函数，这里通过 react 实现了一个一样的渲染函数
 */</span>
<span class="token keyword">import</span> React <span class="token keyword">from</span> <span class="token string">"react"</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> ReactDOM <span class="token keyword">from</span> <span class="token string">"react-dom"</span><span class="token punctuation">;</span>

<span class="token comment">// 渲染主应用</span>
<span class="token keyword">function</span> <span class="token function">Render</span><span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token keyword">const</span> <span class="token punctuation">&#123;</span> loading <span class="token punctuation">&#125;</span> <span class="token operator">=</span> props<span class="token punctuation">;</span>

  <span class="token keyword">return</span> <span class="token punctuation">(</span>
    <span class="token operator">&lt;</span><span class="token operator">></span>
      <span class="token punctuation">&#123;</span>loading <span class="token operator">&amp;&amp;</span> <span class="token operator">&lt;</span>h4 className<span class="token operator">=</span><span class="token string">"subapp-loading"</span><span class="token operator">></span>Loading<span class="token operator">...</span><span class="token operator">&lt;</span><span class="token operator">/</span>h4<span class="token operator">></span><span class="token punctuation">&#125;</span>
      <span class="token operator">&lt;</span>div id<span class="token operator">=</span><span class="token string">"subapp-viewport"</span> <span class="token operator">/</span><span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">/</span><span class="token operator">></span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">// 将主应用渲染到指定节点下</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">render</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">&#123;</span> loading <span class="token punctuation">&#125;</span></span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token keyword">const</span> container <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"subapp-container"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  ReactDOM<span class="token punctuation">.</span><span class="token function">render</span><span class="token punctuation">(</span><span class="token operator">&lt;</span>Render loading<span class="token operator">=</span><span class="token punctuation">&#123;</span>loading<span class="token punctuation">&#125;</span> <span class="token operator">/</span><span class="token operator">></span><span class="token punctuation">,</span> container<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<hr>
<h4 id="VueRender-js"><a href="#VueRender-js" class="headerlink" title="VueRender.js"></a>VueRender.js</h4><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">/**
 * 导出一个由 vue 实现的渲染函数，渲染了一个模版，模版里面包含一个 loading 状态节点和微应用容器节点
 */</span>
<span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">"vue/dist/vue.esm"</span><span class="token punctuation">;</span>

<span class="token comment">// 返回一个 vue 实例</span>
<span class="token keyword">function</span> <span class="token function">vueRender</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">&#123;</span> loading <span class="token punctuation">&#125;</span></span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
    <span class="token literal-property property">template</span><span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">
      &lt;div id="subapp-container">
        &lt;h4 v-if="loading" class="subapp-loading">Loading...&lt;/h4>
        &lt;div id="subapp-viewport">&lt;/div>
      &lt;/div>
    </span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>
    <span class="token literal-property property">el</span><span class="token operator">:</span> <span class="token string">"#subapp-container"</span><span class="token punctuation">,</span>
    <span class="token function">data</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
      <span class="token keyword">return</span> <span class="token punctuation">&#123;</span>
        loading<span class="token punctuation">,</span>
      <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">// vue 实例</span>
<span class="token keyword">let</span> app <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>

<span class="token comment">// 渲染函数</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">render</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">&#123;</span> loading <span class="token punctuation">&#125;</span></span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token comment">// 单例，如果 vue 实例不存在则实例化主应用，存在则说明主应用已经渲染，需要更新主营应用的 loading 状态</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>app<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    app <span class="token operator">=</span> <span class="token function">vueRender</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span> loading <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
    app<span class="token punctuation">.</span>loading <span class="token operator">=</span> loading<span class="token punctuation">;</span>
  <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="手动加载微应用"><a href="#手动加载微应用" class="headerlink" title="手动加载微应用"></a>手动加载微应用</h4><p>通常这种场景下的微应用是一个不带路由的可独立运行的业务组件，这种使用方式的情况比较少见</p>
<h4 id="multiple-js"><a href="#multiple-js" class="headerlink" title="multiple.js"></a>multiple.js</h4><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">import</span> <span class="token punctuation">&#123;</span> loadMicroApp <span class="token punctuation">&#125;</span> <span class="token keyword">from</span> <span class="token string">"../../es"</span><span class="token punctuation">;</span>

<span class="token keyword">let</span> app<span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">mount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  app <span class="token operator">=</span> <span class="token function">loadMicroApp</span><span class="token punctuation">(</span>
    <span class="token punctuation">&#123;</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">"react15"</span><span class="token punctuation">,</span> <span class="token literal-property property">entry</span><span class="token operator">:</span> <span class="token string">"//localhost:7102"</span><span class="token punctuation">,</span> <span class="token literal-property property">container</span><span class="token operator">:</span> <span class="token string">"#react15"</span> <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#123;</span> <span class="token literal-property property">sandbox</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span> <span class="token literal-property property">experimentalStyleIsolation</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">&#125;</span> <span class="token punctuation">&#125;</span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">function</span> <span class="token function">unmount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  app<span class="token punctuation">.</span><span class="token function">unmount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">"#mount"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span> mount<span class="token punctuation">)</span><span class="token punctuation">;</span>
document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">"#unmount"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span> unmount<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">loadMicroApp</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">"vue"</span><span class="token punctuation">,</span> <span class="token literal-property property">entry</span><span class="token operator">:</span> <span class="token string">"//localhost:7101"</span><span class="token punctuation">,</span> <span class="token literal-property property">container</span><span class="token operator">:</span> <span class="token string">"#vue"</span> <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<br />

<hr>
<h3 id="微应用"><a href="#微应用" class="headerlink" title="微应用"></a>微应用</h3><h4 id="vue"><a href="#vue" class="headerlink" title="vue"></a>vue</h4><p>vue 微应用在 <font color="#ec5d3" bgcolor="#fdf5f5">examples&#x2F;vue</font> 目录下，就是一个通过 vue-cli 创建的 vue demo 应用，然后对 <font color="#ec5d3" bgcolor="#fdf5f5">vue.config.js</font> 和 <font color="#ec5d3" bgcolor="#fdf5f5">main.js</font> 做了一些更改</p>
<h4 id="vue-config-js"><a href="#vue-config-js" class="headerlink" title="vue.config.js"></a>vue.config.js</h4><p>需要注意的地方就三点</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token punctuation">&#123;</span>
  <span class="token operator">...</span>
  <span class="token comment">// publicPath 没在这里设置，是通过 webpack 提供的全局变量 __webpack_public_path__ 来即时设置的，webpackjs.com/guides/public-path/</span>
  <span class="token literal-property property">devServer</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    <span class="token operator">...</span>
    <span class="token comment">// 设置跨域，因为主应用需要通过 fetch 去获取微应用引入的静态资源的，所以必须要求这些静态资源支持跨域</span>
    <span class="token literal-property property">headers</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
      <span class="token string-property property">'Access-Control-Allow-Origin'</span><span class="token operator">:</span> <span class="token string">'*'</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  <span class="token literal-property property">output</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    <span class="token literal-property property">library</span><span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">$&#123;</span>name<span class="token interpolation-punctuation punctuation">&#125;</span></span><span class="token string">-[name]</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>	<span class="token comment">// 库名称，唯一</span>
    <span class="token comment">// 将你的 library 暴露为所有的模块定义下都可运行的方式</span>
    <span class="token literal-property property">libraryTarget</span><span class="token operator">:</span> <span class="token string">'umd'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">jsonpFunction</span><span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">webpackJsonp_</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">$&#123;</span>name<span class="token interpolation-punctuation punctuation">&#125;</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span>
  <span class="token operator">...</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="main-js"><a href="#main-js" class="headerlink" title="main.js"></a>main.js</h4><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">// 动态设置 __webpack_public_path__</span>
<span class="token keyword">import</span> <span class="token string">"./public-path"</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> ElementUI <span class="token keyword">from</span> <span class="token string">"element-ui"</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token string">"element-ui/lib/theme-chalk/index.css"</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">"vue"</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> VueRouter <span class="token keyword">from</span> <span class="token string">"vue-router"</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> App <span class="token keyword">from</span> <span class="token string">"./App.vue"</span><span class="token punctuation">;</span>
<span class="token comment">// 路由配置</span>
<span class="token keyword">import</span> routes <span class="token keyword">from</span> <span class="token string">"./router"</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> store <span class="token keyword">from</span> <span class="token string">"./store"</span><span class="token punctuation">;</span>

Vue<span class="token punctuation">.</span>config<span class="token punctuation">.</span>productionTip <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>

Vue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>ElementUI<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">let</span> router <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> instance <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>

<span class="token comment">// 应用渲染函数</span>
<span class="token keyword">function</span> <span class="token function">render</span><span class="token punctuation">(</span><span class="token parameter">props <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span></span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token keyword">const</span> <span class="token punctuation">&#123;</span> container <span class="token punctuation">&#125;</span> <span class="token operator">=</span> props<span class="token punctuation">;</span>
  <span class="token comment">// 实例化 router，根据应用运行环境设置路由前缀</span>
  router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
    <span class="token comment">// 作为微应用运行，则设置 /vue 为前缀，否则设置 /</span>
    <span class="token literal-property property">base</span><span class="token operator">:</span> window<span class="token punctuation">.</span>__POWERED_BY_QIANKUN__ <span class="token operator">?</span> <span class="token string">"/vue"</span> <span class="token operator">:</span> <span class="token string">"/"</span><span class="token punctuation">,</span>
    <span class="token literal-property property">mode</span><span class="token operator">:</span> <span class="token string">"history"</span><span class="token punctuation">,</span>
    routes<span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 实例化 vue 实例</span>
  instance <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
    router<span class="token punctuation">,</span>
    store<span class="token punctuation">,</span>
    <span class="token function-variable function">render</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">h</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">h</span><span class="token punctuation">(</span>App<span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">$mount</span><span class="token punctuation">(</span>container <span class="token operator">?</span> container<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">"#app"</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">// 支持应用独立运行</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>window<span class="token punctuation">.</span>__POWERED_BY_QIANKUN__<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">/**
 * 从 props 中获取通信方法，监听全局状态的更改和设置全局状态，只能操作一级属性
 * @param &#123;*&#125; props
 */</span>
<span class="token keyword">function</span> <span class="token function">storeTest</span><span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  props<span class="token punctuation">.</span>onGlobalStateChange <span class="token operator">&amp;&amp;</span>
    props<span class="token punctuation">.</span><span class="token function">onGlobalStateChange</span><span class="token punctuation">(</span>
      <span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> prev</span><span class="token punctuation">)</span> <span class="token operator">=></span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">[onGlobalStateChange - </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">$&#123;</span>props<span class="token punctuation">.</span>name<span class="token interpolation-punctuation punctuation">&#125;</span></span><span class="token string">]:</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span> value<span class="token punctuation">,</span> prev<span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token boolean">true</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  props<span class="token punctuation">.</span>setGlobalState <span class="token operator">&amp;&amp;</span>
    props<span class="token punctuation">.</span><span class="token function">setGlobalState</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
      <span class="token literal-property property">ignore</span><span class="token operator">:</span> props<span class="token punctuation">.</span>name<span class="token punctuation">,</span>
      <span class="token literal-property property">user</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        <span class="token literal-property property">name</span><span class="token operator">:</span> props<span class="token punctuation">.</span>name<span class="token punctuation">,</span>
      <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">/**
 * 导出的三个生命周期函数
 */</span>
<span class="token comment">// 初始化</span>
<span class="token keyword">export</span> <span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">bootstrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"[vue] vue app bootstraped"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">// 挂载微应用</span>
<span class="token keyword">export</span> <span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">mount</span><span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"[vue] props from main framework"</span><span class="token punctuation">,</span> props<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">storeTest</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">render</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">// 卸载、销毁微应用</span>
<span class="token keyword">export</span> <span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">unmount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  instance<span class="token punctuation">.</span><span class="token function">$destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  instance<span class="token punctuation">.</span>$el<span class="token punctuation">.</span>innerHTML <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>
  instance <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
  router <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="public-path-js"><a href="#public-path-js" class="headerlink" title="public-path.js"></a>public-path.js</h4><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">/**
 * 在入口文件中使用 ES6 模块导入，则在导入后对 __webpack_public_path__ 进行赋值。
 * 在这种情况下，必须将公共路径(public path)赋值移至专属模块，然后将其在最前面导入
 */</span>

<span class="token comment">// qiankun 设置的全局变量，表示应用作为微应用在运行</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>window<span class="token punctuation">.</span>__POWERED_BY_QIANKUN__<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token comment">// eslint-disable-next-line no-undef</span>
  <span class="token comment">// __webpack_public_path__决定了webpack output.publicPath的值，用于来指定应用程序中所有的资源的基本路径</span>
  __webpack_public_path__ <span class="token operator">=</span> window<span class="token punctuation">.</span>__INJECTED_PUBLIC_PATH_BY_QIANKUN__<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="未完待续"><a href="#未完待续" class="headerlink" title="未完待续 ~"></a>未完待续 ~</h2></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://cdn.pluto1811.com/pluto">拾壹baby</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://cdn.pluto1811.com/pluto/2022/09/05/qiankun01/">http://cdn.pluto1811.com/pluto/2022/09/05/qiankun01/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://cdn.pluto1811.com/pluto" target="_blank">Pluto</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="http://cdn.pluto1811.com/goodsImage/1615966170678/1615966002577.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/pluto/2022/09/09/headtag/"><img class="prev-cover" src="http://cdn.pluto1811.com/goodsImage/1615434517323/1615434498647.jpg" onerror="onerror=null;src='/pluto/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">你知道head里一共能写哪几种标签吗？</div></div></a></div><div class="next-post pull-right"><a href="/pluto/2022/09/02/qiankun02/"><img class="next-cover" src="http://cdn.pluto1811.com/goodsImage/1615971759919/1615971739665.jpg" onerror="onerror=null;src='/pluto/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">qiankun源码解读-框架源码篇</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/pluto/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">拾壹baby</div><div class="author-info__description">个人博客</div></div><div class="card-info-data site-data is-center"><a href="/pluto/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/pluto/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/pluto/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%8B%E7%BB%8D"><span class="toc-number">1.</span> <span class="toc-text">介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#single-spa-%E6%9C%89%E5%93%AA%E4%BA%9B%E4%B8%8D%E8%B6%B3%EF%BC%88%E5%85%B6%E5%AE%9E%E4%B9%9F%E6%98%AFqiankun%E7%9A%84%E4%BC%98%E7%82%B9%EF%BC%89"><span class="toc-number">2.</span> <span class="toc-text">single-spa 有哪些不足（其实也是qiankun的优点）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E6%89%93%E5%8C%85%E5%B7%A5%E5%85%B7%E9%85%8D%E7%BD%AE%E7%9A%84%E4%B8%8D%E5%8F%8B%E5%A5%BD"><span class="toc-number">2.1.</span> <span class="toc-text">1. 打包工具配置的不友好</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E6%A0%B7%E5%BC%8F%E9%9A%94%E7%A6%BB%E9%97%AE%E9%A2%98"><span class="toc-number">2.2.</span> <span class="toc-text">2. 样式隔离问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-JS-%E9%9A%94%E7%A6%BB"><span class="toc-number">2.3.</span> <span class="toc-text">3. JS 隔离</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E8%B5%84%E6%BA%90%E9%A2%84%E5%8A%A0%E8%BD%BD"><span class="toc-number">2.4.</span> <span class="toc-text">4. 资源预加载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E5%BA%94%E7%94%A8%E9%97%B4%E9%80%9A%E4%BF%A1"><span class="toc-number">2.5.</span> <span class="toc-text">5. 应用间通信</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B3%BB%E5%88%97%E6%96%87%E7%AB%A0"><span class="toc-number">3.</span> <span class="toc-text">系列文章</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A1%86%E6%9E%B6%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84"><span class="toc-number">4.</span> <span class="toc-text">框架目录结构</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8D%E7%AE%80%E5%8D%95%E7%9A%84-package-json"><span class="toc-number">4.0.1.</span> <span class="toc-text">不简单的 package.json</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B%E9%A1%B9%E7%9B%AE"><span class="toc-number">5.</span> <span class="toc-text">示例项目</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BB%E5%BA%94%E7%94%A8"><span class="toc-number">5.1.</span> <span class="toc-text">主应用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#webpack-config-js"><span class="toc-number">5.1.1.</span> <span class="toc-text">webpack.config.js</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%BA%E4%BA%8E%E8%B7%AF%E7%94%B1%E9%85%8D%E7%BD%AE"><span class="toc-number">5.1.2.</span> <span class="toc-text">基于路由配置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#index-js"><span class="toc-number">5.1.3.</span> <span class="toc-text">index.js</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ReactRender-js"><span class="toc-number">5.1.4.</span> <span class="toc-text">ReactRender.js</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#VueRender-js"><span class="toc-number">5.1.5.</span> <span class="toc-text">VueRender.js</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%8B%E5%8A%A8%E5%8A%A0%E8%BD%BD%E5%BE%AE%E5%BA%94%E7%94%A8"><span class="toc-number">5.1.6.</span> <span class="toc-text">手动加载微应用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#multiple-js"><span class="toc-number">5.1.7.</span> <span class="toc-text">multiple.js</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BE%AE%E5%BA%94%E7%94%A8"><span class="toc-number">5.2.</span> <span class="toc-text">微应用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#vue"><span class="toc-number">5.2.1.</span> <span class="toc-text">vue</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#vue-config-js"><span class="toc-number">5.2.2.</span> <span class="toc-text">vue.config.js</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#main-js"><span class="toc-number">5.2.3.</span> <span class="toc-text">main.js</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#public-path-js"><span class="toc-number">5.2.4.</span> <span class="toc-text">public-path.js</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%AA%E5%AE%8C%E5%BE%85%E7%BB%AD"><span class="toc-number">6.</span> <span class="toc-text">未完待续 ~</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/pluto/2022/10/25/me2022-10/" title="嗯，这两三年...2022年中总结"><img src="http://cdn.pluto1811.com/goodsImage/1650448154299/1650448126499.jpg" onerror="this.onerror=null;this.src='/pluto/img/404.jpg'" alt="嗯，这两三年...2022年中总结"/></a><div class="content"><a class="title" href="/pluto/2022/10/25/me2022-10/" title="嗯，这两三年...2022年中总结">嗯，这两三年...2022年中总结</a><time datetime="2022-10-25T05:20:55.000Z" title="发表于 2022-10-25 13:20:55">2022-10-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/pluto/2022/10/25/promise/" title="Promise里的代码为什么比setTimeout先执行？"><img src="http://cdn.pluto1811.com/goodsImage/1615188358809/376997778.jpg" onerror="this.onerror=null;this.src='/pluto/img/404.jpg'" alt="Promise里的代码为什么比setTimeout先执行？"/></a><div class="content"><a class="title" href="/pluto/2022/10/25/promise/" title="Promise里的代码为什么比setTimeout先执行？">Promise里的代码为什么比setTimeout先执行？</a><time datetime="2022-10-25T05:20:55.000Z" title="发表于 2022-10-25 13:20:55">2022-10-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/pluto/2022/10/23/jsobject/" title="JavaScript对象：啥？对象还有分类？"><img src="http://cdn.pluto1811.com/goodsImage/1615184906664/374751567.jpg" onerror="this.onerror=null;this.src='/pluto/img/404.jpg'" alt="JavaScript对象：啥？对象还有分类？"/></a><div class="content"><a class="title" href="/pluto/2022/10/23/jsobject/" title="JavaScript对象：啥？对象还有分类？">JavaScript对象：啥？对象还有分类？</a><time datetime="2022-10-23T05:20:55.000Z" title="发表于 2022-10-23 13:20:55">2022-10-23</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/pluto/2022/10/19/hexo-ci/" title="【超实用】完美实现基于hexo的博客网站自动化部署"><img src="http://cdn.pluto1811.com/20221020140348.png" onerror="this.onerror=null;this.src='/pluto/img/404.jpg'" alt="【超实用】完美实现基于hexo的博客网站自动化部署"/></a><div class="content"><a class="title" href="/pluto/2022/10/19/hexo-ci/" title="【超实用】完美实现基于hexo的博客网站自动化部署">【超实用】完美实现基于hexo的博客网站自动化部署</a><time datetime="2022-10-19T06:20:58.000Z" title="发表于 2022-10-19 14:20:58">2022-10-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/pluto/2022/10/16/jstyping/" title="JavaScript类型，有可能你不知道这些细节"><img src="http://cdn.pluto1811.com/20221020140348.png" onerror="this.onerror=null;this.src='/pluto/img/404.jpg'" alt="JavaScript类型，有可能你不知道这些细节"/></a><div class="content"><a class="title" href="/pluto/2022/10/16/jstyping/" title="JavaScript类型，有可能你不知道这些细节">JavaScript类型，有可能你不知道这些细节</a><time datetime="2022-10-16T05:20:55.000Z" title="发表于 2022-10-16 13:20:55">2022-10-16</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 拾壹baby</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/pluto/js/utils.js"></script><script src="/pluto/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>